package cn.aixuegao.utils;

import java.util.Map;
import org.springframework.cache.ehcache.EhCacheCacheManager;

import cn.aixuegao.common.constant.BeanNames;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

/**
 * ehcache 工具类
 * 
 * @author hxy
 *
 */
public class EhcacheUtils {
	/**
	 * 缓存管理器
	 */
	private static CacheManager cacheManager = null;
	/**
	 * 缓存
	 */
	private static Cache cache = null;

	/**
	 * 初始化缓存管理器和缓存
	 */
	static {
		EhcacheUtils.initCacheManager();
		EhcacheUtils.initCache("demo");
	}

	/**
	 * 初始化应用默认的缓存
	 */
	public static CacheManager initCache(String CacheName) {
		try {
			if (cache == null)
				cache = cacheManager.getCache(CacheName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cacheManager;
	}

	/**
	 * 
	 * 初始化应用默认缓存管理容器
	 **/
	public static CacheManager initCacheManager() {
		try {
			if (cacheManager == null) {
				EhCacheCacheManager ehCacheCacheManager = (EhCacheCacheManager) SpringBeanUtils
						.getBean(BeanNames.EhCacheCacheManager_BEAN_NAME);
				cacheManager = ehCacheCacheManager.getCacheManager();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cacheManager;
	}

	/**
	 * 存入缓存
	 * @param key
	 * @param value
	 */
	public static void addCache(Object key,Object value) {
		if(cache !=null) {
			Element element = new Element(key,value);
			cache.put(element);
		}
	}
	
	 /** 
     * 获取cache 
     * @param key  关键字 
     * @return 
     */  
    public static Object get(Object key) {  
        Element element = cache.get(key);  
        if (null == element) {  
            return null;  
        }  
        return element.getObjectValue();  
    } 
    
    /**
     * 判断缓存中有没key对应的缓存
     * @param cacheName
     * @param key
     * @return 
     */
    public static boolean containskey(String cacheName, String key) {  
        return cacheManager.getCache(cacheName).isKeyInCache(key);  
    }
    
    /**
     * 判断【系统默认】缓存中有没key对应的缓存
     * @param key
     * @return 
     */
    public static boolean containskey(String key) {  
        return cache.isKeyInCache(key);  
    }
	
    /**
     * 删除【系统默认】缓存中的值
     * @param key
     * @return
     */
    public static boolean removeCache(Object key) {
    	return cache.remove(key);
    }
    
    /**
     * 删除指定的缓存中的值
     * @param cacheName
     * @param key
     * @return
     */
    public static boolean removeCache(String cacheName , Object key) {
    	return cacheManager.getCache(cacheName).remove(key); 
    }
    
    /**
     * 删除所有的缓存
     * @param cacheName
     */
    public static void removeCacheAll() {  
        cache.removeAll();  
    }
    
	/**
	 * 
	 * 初始化缓存管理容器
	 * 
	 * @param path
	 *            ehcache.xml存放的路徑
	 */
	/*public static CacheManager initCacheManager(String path) {

		try {
			CacheManager newCacheManager = null;
			newCacheManager = net.sf.ehcache.CacheManager.getInstance();
			return newCacheManager.create(path);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	 */
	public static void getCache() {
		Map<Object, Element> elements = cache.getAll(cache.getKeys());
		if (elements != null && !elements.isEmpty()) {
			for (Map.Entry<Object, Element> entry : elements.entrySet()) {
				System.out.println("key= " + entry.getValue().getObjectKey());
				System.out.println("value= " + entry.getValue().getObjectValue());
			}
		}else {
			System.out.println("缓存不存在");
		}
	}
}
